using System;
using System.Web.Mail;
using System.Configuration;
using System.Text ; 

namespace SystemFrameworks.Util
{
	/// <summary>
	/// Summary description for MailHelper.
	/// </summary>
	public sealed class MailHelper
	{
		static MailHelper()
		{	
			// Set SMTP relay mail server
			SmtpMail.SmtpServer = getSmtpMailServer();
		}
		
		private MailHelper()
		{	
		}

		#region Const String

		// Key name from web.config for SmtpMailServer 
		private const string SMTP_MAIL_SERVER = "SmtpMailServer";
		//Key name from web.config for Mail Server Alias
		private const string MAIL_SERVER_ALIAS = "MailServerAlias";

		#endregion

		#region Private Method

		private static string getSmtpMailServer()
		{	
			string smtpMailServer = string.Empty;
			object obj = ConfigurationSettings.AppSettings[ SMTP_MAIL_SERVER];			
			if( obj!=null )
			{
				smtpMailServer = ( string )obj;
			}
			return smtpMailServer;
		}
		private static string getMailSenderAlias()
		{	
			string mailSenderAlias = string.Empty;
			object obj = ConfigurationSettings.AppSettings[ MAIL_SERVER_ALIAS];			
			if( obj!=null )
			{
				mailSenderAlias = ( string )obj;
			}
			return mailSenderAlias;
		}

		#endregion	

		/// <summary>
		/// Send mail with MailMessage
		/// </summary>
		/// <param name="mailMsg"></param>
		public static void Send( MailMessage mailMsg )
		{			
			if ( mailMsg == null ) throw new NullReferenceException( " MailMessage is null" );		
			mailMsg.Priority = MailPriority.Normal;
			
			string pMailSenderAlias = getMailSenderAlias();    
			mailMsg.From = String.Format("\"{0}\" <{1}>", pMailSenderAlias, FormHelp.CommonMail ); 
			SmtpMail.Send( mailMsg );
		}
	}
}
